package com.erlang.spirngbootmybatis.controller;

import com.erlang.spirngbootmybatis.entity.Customer;
import com.erlang.spirngbootmybatis.mapper.CustomerMapper;
import com.erlang.spirngbootmybatis.service.CustomerService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * Created by IntelliJ IDEA.
 *
 * @author ZGQ
 */
@Api(tags = "顾客相关接口")
@RestController
@RequestMapping("/api/v1/customer")
public class CustomerController {

    //在CustomerServiceImpl定义了@Service实现类所以可以得到默认首字母小写的对象
    @Autowired
    private CustomerService customerService;

    @Autowired
    private CustomerMapper customerMapper;


    /**
     * 功能描述: customer 保存接口
     */
    @GetMapping("add")
    public Object add(){

        Customer customer = new Customer();
        customer.setAge(11);
        customer.setCreateTime(new Date());
        customer.setName("张三");
        customer.setPhone("1880177");
        int id = customerService.add(customer);

        return id;
    }

    /**
     * 功能描述：查找全部用户
     * 这里和下面是直接调用跳过Servise层，直接到DAO层
     */
    @GetMapping("findAll")
    public Object findAll(){

        return customerMapper.getAll();
    }

    /**
     * 查找单个用户
     */
    @GetMapping("find_by_id")
    public Object findById(long id){
        return customerMapper.findById(id);
    }

    /**
     * 删除单个用户
     */
    @GetMapping("del_by_id")
    public Object delById(long id){
        customerMapper.delete(id);
        return "";
    }

    /**
     *更新用户
     */
    @GetMapping("update")
    public Object update(String name,int id){
        Customer customer = new Customer();
        customer.setName(name);
        customer.setId(id);
        customerMapper.update(customer);
        return "";
    }
}
